package com.wuhongyu.controller;

import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Customer;
import com.wuhongyu.service.CustomerService;
import com.wuhongyu.service.DictionaryService;
import com.wuhongyu.service.ReferService;
import com.wuhongyu.service.impl.DictionaryServiceImpl;
import com.wuhongyu.service.impl.UserServiceImpl;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 客户
 */
@Controller
@RequestMapping("/customer")
public class CustomerController{

    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ReferService referService;

    @RequestMapping("/list")
    public String list(Model model, HttpServletRequest request, Customer customer, Integer number) {
        model.addAttribute("url",MyUtil.getUrl(request));
        model.addAttribute("page",customerService.findAll(customer,number));
        model.addAttribute("areaList",((DictionaryServiceImpl)dictionaryService).findByType(MyCommon.AREA));
        model.addAttribute("gradeList",((DictionaryServiceImpl)dictionaryService).findByType(MyCommon.GRADE));
        return "~cust/cust/list";
    }

    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id,Integer referid) {
        model.addAttribute("userList",userServiceImpl.findAllExit());
        model.addAttribute("areaList",((DictionaryServiceImpl)dictionaryService).findByType(MyCommon.AREA));
        model.addAttribute("gradeList",((DictionaryServiceImpl)dictionaryService).findByType(MyCommon.GRADE));
        if(id!=null && referid ==null){
            model.addAttribute("customer",customerService.findOne(id));
            return "~cust/cust/edit";
        }else{
            model.addAttribute("refer",referService.findOne(referid));
            return "~cust/cust/add";
        }

    }
    @RequestMapping("/save")
    public String save(Model model, HttpServletRequest request, Customer customer) {
        customerService.save(customer);
        return "redirect:/customer/list";
    }
    @RequestMapping("/delete")
    public String delete(Model model, HttpServletRequest request, Integer id) {
        customerService.delete(id);
        return "redirect:/customer/list";
    }
}